package top.yehot.kettle.boot.service.impl;

import java.util.Collections;
import java.util.stream.Collectors;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import top.yehot.cmm.util.mybatis.utils.PageUtil;
import top.yehot.kettle.boot.mapper.TaskRecordMapper;
import top.yehot.kettle.boot.models.entity.TaskRecord;
import top.yehot.kettle.boot.models.vo.task.TaskRecordSearchVO;
import top.yehot.kettle.boot.models.vo.task.TaskRecordVO;
import top.yehot.kettle.boot.service.TaskRecordService;

/**
 * 任务执行记录 服务实现类
 * 
 * @author lquan
 * @date 2022年6月6日 上午1:22:40
 */
@Service
public class TaskRecordServiceImpl extends ServiceImpl<TaskRecordMapper, TaskRecord> implements TaskRecordService {

	@Override
	public IPage<TaskRecordVO> page(TaskRecordSearchVO param) {
		// 分页查询
		IPage<TaskRecord> result = baseMapper.selectPage(PageUtil.of(param), Wrappers.lambdaQuery(TaskRecord.class)
				.eq(TaskRecord::getTaskId, param.getTaskId()).orderByDesc(TaskRecord::getCreateTime));
		
		// 重新封装
		IPage<TaskRecordVO> page = new Page<>(result.getCurrent(), result.getSize(), result.getTotal(), result.searchCount());
		page.setPages(result.getPages());
		page.setRecords(
			result.getRecords() == null ? Collections.emptyList() : result.getRecords().stream().map(e -> {
				TaskRecordVO vo = new TaskRecordVO();
				BeanUtils.copyProperties(e, vo);
				return vo;
			}).collect(Collectors.toList())
		);
		return page;
	}

}
